package com.example.fengyungo.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo.R;
import com.example.fengyungo.activity.BaseActivity;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.ResultConstant;
import com.example.fengyungo.constant.TextConstant;
import com.example.fengyungo.domain.Driver;
import com.example.fengyungo.utils.OkHttpUtils;
import com.example.fengyungo.utils.SharedPreferencesUtils;
import com.example.fengyungo.utils.ThreadPoolUtil;
import com.google.android.material.textfield.TextInputLayout;

import java.io.IOException;

public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener {

    private TextInputLayout oldPasswordTil;

    private TextInputLayout newPasswordTil;

    private TextInputLayout okPasswordTil;

    private EditText oldPasswordEt;

    private EditText newPasswordEt;

    private EditText okPasswordEt;

    private Button okBtn;

    private ImageButton backIb;

    private ProgressBar progressBar;

    private String oldPassword;

    private String newPassword;

    private String okPassword;

    private Driver driver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        // 获取司机信息
        driver = SharedPreferencesUtils.getDriverInfo(this);

        initControls();

        // 获取输入的原密码与新密码


        // 点击事件
        backIb.setOnClickListener(this);
        okBtn.setOnClickListener(this);
    }

    private void initControls() {
        oldPasswordEt = findViewById(R.id.et_old_password);
        newPasswordEt = findViewById(R.id.et_new_password);
        okPasswordEt = findViewById(R.id.et_ok_password);

        oldPasswordTil = findViewById(R.id.input_old_password);
        newPasswordTil = findViewById(R.id.input_new_password);
        okPasswordTil = findViewById(R.id.input_ok_password);

        okBtn = findViewById(R.id.ok);
        backIb = findViewById(R.id.back);

        progressBar = findViewById(R.id.progress_bar);
    }

    /**
     * 获取输入的密码
     */
    private void getPasswordInput() {
        oldPassword = oldPasswordEt.getText().toString();
        newPassword = newPasswordEt.getText().toString();
        okPassword = okPasswordEt.getText().toString();
    }

    /**
     * 点击事件
     *
     * @param view View
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back) {
            // 返回
            finish();
        } else if (id == R.id.ok) {
            // 修改密码
            changePassword();
        }
    }

    /**
     * 修改密码具体操作
     */
    private void changePassword() {
        boolean cancel = false;
        View focusView = null;

        // 获取输入框输入的密码
        getPasswordInput();

        oldPasswordTil.setError(null);
        newPasswordTil.setError(null);
        okPasswordTil.setError(null);

        if (oldPassword.isEmpty()) {
            oldPasswordTil.setError(TextConstant.INPUT_OLD_PASSWORD);
            focusView = oldPasswordEt;
            cancel = true;
        } else if (newPassword.isEmpty()) {
            newPasswordTil.setError(TextConstant.INPUT_NEW_PASSWORD);
            focusView = newPasswordEt;
            cancel = true;
        } else if (okPassword.isEmpty()) {
            okPasswordTil.setError(TextConstant.INPUT_OK_PASSWORD);
            focusView = okPasswordEt;
            cancel = true;
        }

        if (cancel) {
            // 出现错误的账号或者密码的View获得焦点
            focusView.requestFocus();
        } else {
            // 密码与确认密码相同
            if (newPassword.equals(okPassword)) {
                changePasswordWithPut();
            }
            // 密码与确认密码不相同
            else {
                Toast.makeText(this, TextConstant.INPUT_PASSWORD_DIFFERENT_TWICE, Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 发送请求修改密码
     */
    private void changePasswordWithPut() {
        progressBar.setVisibility(View.VISIBLE);
        Runnable runnable = () -> {
            String url = DriverConstant.URL_USER_DRIVER + "/changePassword/" +
                    driver.getUsername() + "/" + oldPassword + "/" + newPassword;
            try {
                String responseData = OkHttpUtils.put(url, "");
                JSONObject jsonObject = JSONObject.parseObject(responseData);
                int code = jsonObject.getInteger(ResultConstant.CODE);
                String msg = jsonObject.getString(ResultConstant.MSG);
                if (code == ResultConstant.SUCCESS) {
                    runOnUiThread(() -> {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(ChangePasswordActivity.this, TextConstant.PASSWORD_CHANGE_SUCCESS, Toast.LENGTH_SHORT).show();
                        finish();
                    });
                } else {
                    runOnUiThread(() -> {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(ChangePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(ChangePasswordActivity.this, TextConstant.INTERNET_ERROR, Toast.LENGTH_SHORT).show();
                });
            }
        };
        ThreadPoolUtil.execute(runnable);
    }
}